home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / virus / stelth22.zip / BUFALLOC.C < prev    next >
C/C++ Source or Header  |  1992-02-10  |  964b  |  43 lines

  1. /*
  2. bufalloc.c
  3. Stealth Bomber Version 2.2
  4.  
  5. Kevin Dean
  6. Fairview Mall P.O. Box 55074
  7. 1800 Sheppard Avenue East
  8. Willowdale, Ontario
  9. CANADA    M2J 5B9
  10. CompuServe ID: 76336,3114
  11.  
  12. February 10, 1992
  13.  
  14.     This module allocates a simple memory buffer whose size depends on the
  15. amount of memory available.  The size of the buffer is halved each time the
  16. allocation fails until memory is successfully allocated or the size goes below
  17. the minimum size requested.
  18.  
  19.     This code is public domain.
  20. */
  21.  
  22.  
  23. #include <stdlib.h>
  24.  
  25. #include "vircheck.h"
  26.  
  27.  
  28. /***/
  29. /* Allocate a buffer of flexible size. */
  30. void *bufalloc(size_t *size, size_t minsize)
  31. {
  32. void *buffer;    /* Buffer allocated. */
  33. size_t bufsize;    /* Size of buffer allocated. */
  34.  
  35. /* Allocate as big a buffer as possible (at least minsize). */
  36. for (bufsize = *size; bufsize >= minsize && !(buffer = malloc(bufsize)); bufsize /= 2);
  37.  
  38. /* Save buffer size. */
  39. *size = bufsize;
  40.  
  41. return (buffer);
  42. }
  43.